using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.IO;

namespace LS.Editor
{
    public class DataTableMenu
    {
        private static readonly string configTemplatePath = "Packages/com.lancerstudio.lsframework/Editor/Assets/CodeTemplate/ConfigTemplate.csv.txt";
        private static readonly string dictionaryTemplatePath = "Packages/com.lancerstudio.lsframework/Editor/Assets/CodeTemplate/DictionaryTemplate.xml.txt";

        [MenuItem("Assets/Create/Lancer Studio/Empty Data table CSV")]
        public static void CreateEmptyDataTable()
        {
            ProjectWindowUtil.CreateScriptAssetFromTemplateFile(configTemplatePath, "New Config.csv");
        }

        [MenuItem("Assets/Create/Lancer Studio/Empty Dictionary XML")]
        public static void CreateNewDictionary()
        {
            ProjectWindowUtil.CreateScriptAssetFromTemplateFile(dictionaryTemplatePath, "New Dictionary.xml");
        }

        [MenuItem("Assets/Create/Lancer Studio/Data Table CSV/Entity")]
        public static void CreateEntityDataTable()
        {
            string[] keys = new string[] { "AssetName", "EntityGroupName" };
            string[] types = new string[] { "string", "string" };
            string[] notes = new string[] { "资源名称","实体组名称" };
            CreateCustomDataTable("Entity.csv", keys, types, notes);
        }

        [MenuItem("Assets/Create/Lancer Studio/Data Table CSV/Scene")]
        public static void CreateSceneDataTable()
        {
            string[] keys = new string[] { "AssetName", "BackgroundMusicId" };
            string[] types = new string[] { "string", "int" };
            string[] notes = new string[] { "资源名称", "背景音乐编号" };
            CreateCustomDataTable("Scene.csv", keys, types, notes);
        }

        [MenuItem("Assets/Create/Lancer Studio/Data Table CSV/Sound")]
        public static void CreateSoundDataTable()
        {
            string[] keys = new string[] { "AssetName", "Priority", "Loop", "Volume", "SpatialBlend", "MaxDistance" };
            string[] types = new string[] { "string", "int", "bool", "float", "float", "float" };
            string[] notes = new string[] { "资源名称", "优先级(默认0,取值范围-128至128)", "是否循环", "音量（0-1）", "声音空间混合度（0为2D，1为3D，中间值混合效果）", "3D声音最大距离" };
            CreateCustomDataTable("Sound.csv", keys, types, notes);
        }

        [MenuItem("Assets/Create/Lancer Studio/Data Table CSV/Music")]
        public static void CreateMusicDataTable()
        {
            string[] keys = new string[] { "AssetName" };
            string[] types = new string[] { "string" };
            string[] notes = new string[] { "资源名称" };
            CreateCustomDataTable("Music.csv", keys, types, notes);
        }

        [MenuItem("Assets/Create/Lancer Studio/Data Table CSV/UIForm")]
        public static void CreateUIFormDataTable()
        {
            string[] keys = new string[] { "AssetName", "UIGroupName", "AllowMultilnstance", "PauseCoveredUIForm" };
            string[] types = new string[] { "string", "string", "bool", "bool" };
            string[] notes = new string[] { "资源名称", "界面组名称", "是否允许多个界面实例", "是否暂停被其覆盖的界面" };
            CreateCustomDataTable("UIForm.csv", keys, types, notes);
        }

        [MenuItem("Assets/Create/Lancer Studio/Data Table CSV/UISound")]
        public static void CreateUISoundDataTable()
        {
            string[] keys = new string[] { "AssetName", "Priority", "Volume" };
            string[] types = new string[] { "string", "int", "float" };
            string[] notes = new string[] { "资源名称", "优先级（默认0，取值范围-128至128）", "音量（0-1）" };
            CreateCustomDataTable("UISound.csv", keys, types, notes);
        }

        public static void CreateCustomDataTable(string fileName, string[] keys, string[] types, string[] notes)
        {
            string[] _2 = new string[] { "Id", "" }.Concat(keys).ToArray();
            string[] _3 = new string[] { "int", "" }.Concat(types).ToArray();
            string[] _4 = new string[] { "编号", "备注" }.Concat(notes).ToArray();

            string name = Path.GetFileNameWithoutExtension(fileName);
            ProjectWindowUtil.CreateAssetWithContent(fileName, CSVUtility.CombinBaseText(name, CSVUtility.Table, _2, _3, _4));
        }
    }
}
